#include "include/dictionarylookup.h"

#include "include/Common.h"

#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <assert.h>

QString DictionaryLookupThreaded::m_cacheDirectory = getHome() + "/.dictionary/";

QSet<QString> DictionaryLookup::m_words = QSet<QString>();

DictionaryLookupThreaded::DictionaryLookupThreaded(const QString& word, const QString& server):
	m_word(word),
	m_server(server)
{

}

void DictionaryLookupThreaded::run()
{
	static bool cacheDirectoryChecked = false;
	if (!cacheDirectoryChecked)
		checkAndCreateCacheDir();

	QString dictionaryOutput;

	QFile definitionFile(m_cacheDirectory + m_word.toLower().toAscii() + "." + m_server);

	if (definitionFile.exists())
	{
		definitionFile.open(QIODevice::ReadOnly | QIODevice::Text);

		QTextStream in(&definitionFile);
		dictionaryOutput = in.readAll();
	}
	else
	{
		dictionaryOutput = sysExec(QString("dict -d " + m_server + " \"" + m_word + "\"").toUtf8());
		definitionFile.open(QIODevice::WriteOnly | QIODevice::Text);
			QTextStream out(&definitionFile);
			out << dictionaryOutput;
	}
	gotDefinition(dictionaryOutput);
	deleteLater();
}

void DictionaryLookupThreaded::checkAndCreateCacheDir()
{
	QDir dir(m_cacheDirectory);
	if (!dir.exists())
		QDir().mkpath(m_cacheDirectory);
}

//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////

DictionaryLookup::DictionaryLookup(const QString& word):
	m_word(word.toLower())
{

}

void DictionaryLookup::performLookup()
{
	if (exists())
	{
		DictionaryLookupThreaded* pLookup = new DictionaryLookupThreaded(m_word, "wn");
		connect(pLookup, SIGNAL(gotDefinition(const QString&)), this, SLOT(gotDefinitionSlot(const QString&)));
		pLookup->start();
	}
}

void DictionaryLookup::performSynonymsLookup()
{
	if (exists())
	{
		DictionaryLookupThreaded* pLookup = new DictionaryLookupThreaded(m_word, "moby-thes");
		connect(pLookup, SIGNAL(gotDefinition(const QString&)), this, SLOT(gotSynonymsSlot(const QString&)));
		pLookup->start();
	}
}

QString DictionaryLookup::getRawLookup() const
{
	return m_dictionaryOutput;
}

QString DictionaryLookup::getRawSynonyms() const
{
	return m_synonymsOutput;
}

bool DictionaryLookup::exists()
{
	static bool dictionaryInitialised = false;

	if (!dictionaryInitialised)
	{
		QFile file(":/resources/dictionary.txt");
		assert(file.open(QIODevice::ReadOnly | QIODevice::Text));

		QTextStream in(&file);
		while (!in.atEnd())
		{
			QString line = in.readLine();
			m_words.insert(line);
		}
		dictionaryInitialised = true;
	}
	return m_words.contains(m_word);
}

void DictionaryLookup::gotDefinitionSlot(const QString& str)
{
	m_dictionaryOutput = str;
	gotDefinition(this);
}

void DictionaryLookup::gotSynonymsSlot(const QString& str)
{
	m_synonymsOutput = str;
	gotSynonyms(this);
}
